package com.hcm.sk.validator;

import com.hcm.sk.util.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @description: IsMobileValidator
 * @date: 2021/1/25 22:07
 * @author: hcm
 * @version: 1.0
 */
public class IsMobileValidator implements ConstraintValidator<isMobile,String> {
    private boolean required = false;
    @Override
    public void initialize(isMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {

        if(required){
            return ValidatorUtil.isMobile(s);
        }else {
            if(StringUtils.isEmpty(s)){
                return  true;
            }else {
                return ValidatorUtil.isMobile(s);
            }
        }
    }
}
